<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>服务端渲染 | Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.3.3">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
    

        
    
    
    <link rel="next" href="../../docs/recipes/WritingTests.html" />
    
    
    <link rel="prev" href="../../docs/recipes/ReducingBoilerplate.html" />
    

        
    </head>
    <body>
        
        
    <div class="book" data-level="4.4" data-basepath="../.." data-revision="Thu Mar 16 2017 23:41:56 GMT+0800 (CST)">
    

<div class="book-summary">
    <div class="book-search">
        <input type="text" placeholder="Type to search" class="form-control" />
    </div>
    <ul class="summary">
        
        
        
        

        

        
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        自述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="docs/introduction/index.html">
            
                
                    <a href="../../docs/introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="docs/introduction/Motivation.html">
            
                
                    <a href="../../docs/introduction/Motivation.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        动机
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="docs/introduction/CoreConcepts.html">
            
                
                    <a href="../../docs/introduction/CoreConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        核心概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="docs/introduction/ThreePrinciples.html">
            
                
                    <a href="../../docs/introduction/ThreePrinciples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        三大原则
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="docs/introduction/PriorArt.html">
            
                
                    <a href="../../docs/introduction/PriorArt.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        先前技术
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="docs/introduction/Ecosystem.html">
            
                
                    <a href="../../docs/introduction/Ecosystem.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        生态系统
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="docs/introduction/Examples.html">
            
                
                    <a href="../../docs/introduction/Examples.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        示例
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="docs/basics/index.html">
            
                
                    <a href="../../docs/basics/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="docs/basics/Actions.html">
            
                
                    <a href="../../docs/basics/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="docs/basics/Reducers.html">
            
                
                    <a href="../../docs/basics/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="docs/basics/Store.html">
            
                
                    <a href="../../docs/basics/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="docs/basics/DataFlow.html">
            
                
                    <a href="../../docs/basics/DataFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="docs/basics/UsageWithReact.html">
            
                
                    <a href="../../docs/basics/UsageWithReact.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        搭配 React
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="docs/basics/ExampleTodoList.html">
            
                
                    <a href="../../docs/basics/ExampleTodoList.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        示例：Todo List
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="docs/advanced/index.html">
            
                
                    <a href="../../docs/advanced/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="docs/advanced/AsyncActions.html">
            
                
                    <a href="../../docs/advanced/AsyncActions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        异步 Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="docs/advanced/AsyncFlow.html">
            
                
                    <a href="../../docs/advanced/AsyncFlow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        异步数据流
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="docs/advanced/Middleware.html">
            
                
                    <a href="../../docs/advanced/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Middleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="docs/advanced/UsageWithReactRouter.html">
            
                
                    <a href="../../docs/advanced/UsageWithReactRouter.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        搭配 React Router
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="docs/advanced/ExampleRedditAPI.html">
            
                
                    <a href="../../docs/advanced/ExampleRedditAPI.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        示例：Reddit API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="docs/advanced/NextSteps.html">
            
                
                    <a href="../../docs/advanced/NextSteps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        下一步
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="docs/recipes/index.html">
            
                
                    <a href="../../docs/recipes/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        技巧
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="docs/recipes/MigratingToRedux.html">
            
                
                    <a href="../../docs/recipes/MigratingToRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        迁移到 Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="docs/recipes/UsingObjectSpreadOperator.html">
            
                
                    <a href="../../docs/recipes/UsingObjectSpreadOperator.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        使用对象展开运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="docs/recipes/ReducingBoilerplate.html">
            
                
                    <a href="../../docs/recipes/ReducingBoilerplate.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        减少样板代码
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.4" data-path="docs/recipes/ServerRendering.html">
            
                
                    <a href="../../docs/recipes/ServerRendering.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        服务端渲染
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="docs/recipes/WritingTests.html">
            
                
                    <a href="../../docs/recipes/WritingTests.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        编写测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="docs/recipes/ComputingDerivedData.html">
            
                
                    <a href="../../docs/recipes/ComputingDerivedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        计算衍生数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="docs/recipes/ImplementingUndoHistory.html">
            
                
                    <a href="../../docs/recipes/ImplementingUndoHistory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.7.</b>
                        
                        实现撤销重做
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="docs/recipes/IsolatingSubapps.html">
            
                
                    <a href="../../docs/recipes/IsolatingSubapps.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.8.</b>
                        
                        子应用隔离
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="docs/recipes/StructuringReducers.html">
            
                
                    <a href="../../docs/recipes/StructuringReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.</b>
                        
                        组织 Reducer
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.9.1" data-path="docs/recipes/reducers/PrerequisiteConcepts.html">
            
                
                    <a href="../../docs/recipes/reducers/PrerequisiteConcepts.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.1.</b>
                        
                        Reducer 基础概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.2" data-path="docs/recipes/reducers/BasicReducerStructure.html">
            
                
                    <a href="../../docs/recipes/reducers/BasicReducerStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.2.</b>
                        
                        Reducer 基础结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.3" data-path="docs/recipes/reducers/SplittingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/SplittingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.3.</b>
                        
                        Reducer 逻辑拆分
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.4" data-path="docs/recipes/reducers/RefactoringReducersExample.html">
            
                
                    <a href="../../docs/recipes/reducers/RefactoringReducersExample.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.4.</b>
                        
                        Reducer 重构示例
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.5" data-path="docs/recipes/reducers/UsingCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/UsingCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.5.</b>
                        
                        `combineReducers` 用法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.6" data-path="docs/recipes/reducers/BeyondCombineReducers.html">
            
                
                    <a href="../../docs/recipes/reducers/BeyondCombineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.6.</b>
                        
                        `combineReducers` 进阶
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.7" data-path="docs/recipes/reducers/NormalizingStateShape.html">
            
                
                    <a href="../../docs/recipes/reducers/NormalizingStateShape.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.7.</b>
                        
                        State 范式化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.8" data-path="docs/recipes/reducers/UpdatingNormalizedData.html">
            
                
                    <a href="../../docs/recipes/reducers/UpdatingNormalizedData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.8.</b>
                        
                        管理范式化数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.9" data-path="docs/recipes/reducers/ReusingReducerLogic.html">
            
                
                    <a href="../../docs/recipes/reducers/ReusingReducerLogic.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.9.</b>
                        
                        Reducer 逻辑复用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.10" data-path="docs/recipes/reducers/ImmutableUpdatePatterns.html">
            
                
                    <a href="../../docs/recipes/reducers/ImmutableUpdatePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.10.</b>
                        
                        不可变更新模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.9.11" data-path="docs/recipes/reducers/InitializingState.html">
            
                
                    <a href="../../docs/recipes/reducers/InitializingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.9.11.</b>
                        
                        初始化 State
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="docs/FAQ.html">
            
                
                    <a href="../../docs/FAQ.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        常见问题
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="docs/faq/General.html">
            
                
                    <a href="../../docs/faq/General.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        综合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="docs/faq/Reducers.html">
            
                
                    <a href="../../docs/faq/Reducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        Reducer
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="docs/faq/OrganizingState.html">
            
                
                    <a href="../../docs/faq/OrganizingState.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        组织 State
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="docs/faq/StoreSetup.html">
            
                
                    <a href="../../docs/faq/StoreSetup.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        创建 Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="docs/faq/Actions.html">
            
                
                    <a href="../../docs/faq/Actions.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        Action
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="docs/faq/CodeStructure.html">
            
                
                    <a href="../../docs/faq/CodeStructure.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        代码结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="docs/faq/Performance.html">
            
                
                    <a href="../../docs/faq/Performance.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        性能
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.8" data-path="docs/faq/ReactRedux.html">
            
                
                    <a href="../../docs/faq/ReactRedux.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.8.</b>
                        
                        React Redux
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.9" data-path="docs/faq/Miscellaneous.html">
            
                
                    <a href="../../docs/faq/Miscellaneous.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.9.</b>
                        
                        其它
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="docs/Troubleshooting.html">
            
                
                    <a href="../../docs/Troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        排错
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="docs/Glossary.html">
            
                
                    <a href="../../docs/Glossary.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        词汇表
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="docs/api/index.html">
            
                
                    <a href="../../docs/api/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        API 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="docs/api/createStore.html">
            
                
                    <a href="../../docs/api/createStore.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        createStore
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="docs/api/Store.html">
            
                
                    <a href="../../docs/api/Store.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        Store
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="docs/api/combineReducers.html">
            
                
                    <a href="../../docs/api/combineReducers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        combineReducers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="docs/api/applyMiddleware.html">
            
                
                    <a href="../../docs/api/applyMiddleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        applyMiddleware
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="docs/api/bindActionCreators.html">
            
                
                    <a href="../../docs/api/bindActionCreators.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        bindActionCreators
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="docs/api/compose.html">
            
                
                    <a href="../../docs/api/compose.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        compose
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="docs/react-redux/index.html">
            
                
                    <a href="../../docs/react-redux/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        react-redux 文档
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="docs/react-redux/api.html">
            
                
                    <a href="../../docs/react-redux/api.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        API
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="docs/react-redux/troubleshooting.html">
            
                
                    <a href="../../docs/react-redux/troubleshooting.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        排错
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" >
            
                
                    <a target="_blank" href="https:/github.com/react-guide/redux-tutorial-cn">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        redux-tutorial
                    </a>
            
            
        </li>
    


        
        <li class="divider"></li>
        <li>
            <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                Published with GitBook
            </a>
        </li>
        
    </ul>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header">
    <!-- Actions Left -->
    <a href="#" class="btn pull-left toggle-summary" aria-label="Table of Contents"><i class="fa fa-align-justify"></i></a>
    <a href="#" class="btn pull-left toggle-search" aria-label="Search"><i class="fa fa-search"></i></a>
    
    <div id="font-settings-wrapper" class="dropdown pull-left">
        <a href="#" class="btn toggle-dropdown" aria-label="Font Settings"><i class="fa fa-font"></i>
        </a>
        <div class="dropdown-menu font-settings">
    <div class="dropdown-caret">
        <span class="caret-outer"></span>
        <span class="caret-inner"></span>
    </div>

    <div class="buttons">
        <button type="button" id="reduce-font-size" class="button size-2">A</button>
        <button type="button" id="enlarge-font-size" class="button size-2">A</button>
    </div>

    <div class="buttons font-family-list">
        <button type="button" data-font="0" class="button">Serif</button>
        <button type="button" data-font="1" class="button">Sans</button>
    </div>

    <div class="buttons color-theme-list">
        <button type="button" id="color-theme-preview-0" class="button size-3" data-theme="0">White</button>
        <button type="button" id="color-theme-preview-1" class="button size-3" data-theme="1">Sepia</button>
        <button type="button" id="color-theme-preview-2" class="button size-3" data-theme="2">Night</button>
    </div>
</div>

    </div>

    <!-- Actions Right -->
    
    <div class="dropdown pull-right">
        <a href="#" class="btn toggle-dropdown" aria-label="Share"><i class="fa fa-share-alt"></i>
        </a>
        <div class="dropdown-menu font-settings dropdown-left">
            <div class="dropdown-caret">
                <span class="caret-outer"></span>
                <span class="caret-inner"></span>
            </div>
            <div class="buttons">
                <button type="button" data-sharing="twitter" class="button">
                    Share on Twitter
                </button>
                <button type="button" data-sharing="google-plus" class="button">
                    Share on Google
                </button>
                <button type="button" data-sharing="facebook" class="button">
                    Share on Facebook
                </button>
                <button type="button" data-sharing="weibo" class="button">
                    Share on Weibo
                </button>
                <button type="button" data-sharing="instapaper" class="button">
                    Share on Instapaper
                </button>
            </div>
        </div>
    </div>
    

    
    <a href="#" target="_blank" class="btn pull-right google-plus-sharing-link sharing-link" data-sharing="google-plus" aria-label="Google"><i class="fa fa-google-plus"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right facebook-sharing-link sharing-link" data-sharing="facebook" aria-label="Facebook"><i class="fa fa-facebook"></i></a>
    
    
    <a href="#" target="_blank" class="btn pull-right twitter-sharing-link sharing-link" data-sharing="twitter" aria-label="Twitter"><i class="fa fa-twitter"></i></a>
    
    
    


    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Redux 中文文档 Join the chat at https://gitter.im/camsong/redux-in-chinese</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <a id="edit-link" href="https://github.com/camsong/redux-in-chinese/tree/master/docs/recipes/ServerRendering.md" class="btn fa fa-edit pull-left">&#xA0;&#xA0;&#x5F00;&#x59CB;&#x7EA0;&#x9519;</a><h1 id="%E6%9C%8D%E5%8A%A1%E7%AB%AF%E6%B8%B2%E6%9F%93">&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;</h1>
<p>&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x4E00;&#x4E2A;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x573A;&#x666F;&#x662F;&#x5F53;&#x7528;&#x6237;&#xFF08;&#x6216;&#x641C;&#x7D22;&#x5F15;&#x64CE;&#x722C;&#x866B;&#xFF09;&#x7B2C;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x7528;&#x5B83;&#x6765;&#x505A;<em>&#x521D;&#x59CB;&#x6E32;&#x67D3;</em>&#x3002;&#x5F53;&#x670D;&#x52A1;&#x5668;&#x63A5;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x540E;&#xFF0C;&#x5B83;&#x628A;&#x9700;&#x8981;&#x7684;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210; HTML &#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7136;&#x540E;&#x628A;&#x5B83;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#xFF08;&#x8FD9;&#x91CC;&#x7EDF;&#x6307;&#x6D4F;&#x89C8;&#x5668;&#xFF09;&#x3002;&#x4E4B;&#x540E;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x4F1A;&#x63A5;&#x624B;&#x6E32;&#x67D3;&#x63A7;&#x5236;&#x6743;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x6211;&#x4EEC;&#x4F7F;&#x7528; React &#x6765;&#x505A;&#x793A;&#x4F8B;&#xFF0C;&#x5BF9;&#x4E8E;&#x652F;&#x6301;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x5176;&#x5B83; view &#x6846;&#x67B6;&#xFF0C;&#x505A;&#x6CD5;&#x4E5F;&#x662F;&#x7C7B;&#x4F3C;&#x7684;&#x3002;</p>
<h3 id="%E6%9C%8D%E5%8A%A1%E7%AB%AF%E4%BD%BF%E7%94%A8-redux">&#x670D;&#x52A1;&#x7AEF;&#x4F7F;&#x7528; Redux</h3>
<p>&#x5F53;&#x5728;&#x670D;&#x52A1;&#x5668;&#x4F7F;&#x7528; Redux &#x6E32;&#x67D3;&#x65F6;&#xFF0C;&#x4E00;&#x5B9A;&#x8981;&#x5728;&#x54CD;&#x5E94;&#x4E2D;&#x5305;&#x542B;&#x5E94;&#x7528;&#x7684; state&#xFF0C;&#x8FD9;&#x6837;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x628A;&#x5B83;&#x4F5C;&#x4E3A;&#x521D;&#x59CB; state&#x3002;&#x8FD9;&#x70B9;&#x81F3;&#x5173;&#x91CD;&#x8981;&#xFF0C;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x5728;&#x751F;&#x6210; HTML &#x524D;&#x9884;&#x52A0;&#x8F7D;&#x4E86;&#x6570;&#x636E;&#xFF0C;&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5BA2;&#x6237;&#x7AEF;&#x4E5F;&#x80FD;&#x8BBF;&#x95EE;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x751F;&#x6210;&#x7684; HTML &#x4E0E;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8FD4;&#x56DE;&#x7684; HTML &#x5C31;&#x4F1A;&#x4E0D;&#x5339;&#x914D;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x8FD8;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x3002;</p>
<p>&#x628A;&#x6570;&#x636E;&#x53D1;&#x9001;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x9700;&#x8981;&#x4EE5;&#x4E0B;&#x6B65;&#x9AA4;&#xFF1A;</p>
<ul>
<li>&#x4E3A;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x521B;&#x5EFA;&#x5168;&#x65B0;&#x7684; Redux store &#x5B9E;&#x4F8B;&#xFF1B;</li>
<li>&#x6309;&#x9700; dispatch &#x4E00;&#x4E9B; action&#xFF1B;</li>
<li>&#x4ECE; store &#x4E2D;&#x53D6;&#x51FA; state&#xFF1B;</li>
<li>&#x628A; state &#x4E00;&#x540C;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;</li>
</ul>
<p>&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x4F7F;&#x7528;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684; state &#x521B;&#x5EFA;&#x5E76;&#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x5168;&#x65B0;&#x7684; Redux store&#x3002;<br>Redux &#x5728;&#x670D;&#x52A1;&#x7AEF;<strong>&#x60DF;&#x4E00;</strong>&#x8981;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x5C31;&#x662F;&#xFF0C;&#x63D0;&#x4F9B;&#x5E94;&#x7528;&#x6240;&#x9700;&#x7684;<strong>&#x521D;&#x59CB; state</strong>&#x3002;</p>
<h2 id="%E5%AE%89%E8%A3%85">&#x5B89;&#x88C5;</h2>
<p>&#x4E0B;&#x9762;&#x6765;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x914D;&#x7F6E;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x3002;&#x4F7F;&#x7528;&#x6781;&#x7B80;&#x7684; <a href="https://github.com/rackt/redux/tree/master/examples/counter" target="_blank">Counter &#x8BA1;&#x6570;&#x5668;&#x5E94;&#x7528;</a> &#x6765;&#x505A;&#x793A;&#x4F8B;&#xFF0C;&#x4ECB;&#x7ECD;&#x5982;&#x4F55;&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#x63D0;&#x524D;&#x6E32;&#x67D3; state&#x3002;</p>
<h3 id="%E5%AE%89%E8%A3%85%E4%BE%9D%E8%B5%96%E5%BA%93">&#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x5E93;</h3>
<p>&#x672C;&#x4F8B;&#x4F1A;&#x4F7F;&#x7528; <a href="http://expressjs.com/" target="_blank">Express</a> &#x6765;&#x505A;&#x5C0F;&#x578B;&#x7684; web &#x670D;&#x52A1;&#x5668;&#x3002;&#x8FD8;&#x9700;&#x8981;&#x5B89;&#x88C5; Redux &#x5BF9; React &#x7684;&#x7ED1;&#x5B9A;&#x5E93;&#xFF0C;Redux &#x9ED8;&#x8BA4;&#x5E76;&#x4E0D;&#x5305;&#x542B;&#x3002;</p>
<pre><code>npm install <span class="token operator">--</span>save express react<span class="token operator">-</span>redux
</code></pre><h2 id="%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%BC%80%E5%8F%91">&#x670D;&#x52A1;&#x7AEF;&#x5F00;&#x53D1;</h2>
<p>&#x4E0B;&#x9762;&#x662F;&#x670D;&#x52A1;&#x7AEF;&#x4EE3;&#x7801;&#x5927;&#x6982;&#x7684;&#x6837;&#x5B50;&#x3002;&#x4F7F;&#x7528; <a href="http://expressjs.com/api.html#app.use" target="_blank">app.use</a> &#x6302;&#x8F7D; <a href="http://expressjs.com/guide/using-middleware.html" target="_blank">Express middleware</a> &#x5904;&#x7406;&#x6240;&#x6709;&#x8BF7;&#x6C42;&#x3002;&#x5982;&#x679C;&#x4F60;&#x8FD8;&#x4E0D;&#x719F;&#x6089; Express &#x6216;&#x8005; middleware&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x4E86;&#x89E3;&#x6BCF;&#x6B21;&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x65F6;&#x90FD;&#x4F1A;&#x8C03;&#x7528; handleRender &#x51FD;&#x6570;&#x3002;</p>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5982;&#x679C;&#x6709;&#x4F7F;&#x7528; ES6 &#x548C; JSX &#x8BED;&#x6CD5;&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528; <a href="https://babeljs.io/" target="_blank">Babel</a> (&#x5BF9;&#x5E94;&#x793A;&#x4F8B;<a href="https://github.com/babel/example-node-server" target="_blank">this example of a Node Server with Babel</a>) &#x548C; <a href="https://babeljs.io/docs/plugins/preset-react/" target="_blank">React preset</a>&#x3002;</p>
<h5 id="-server-js"><code>server<span class="token punctuation">.</span>js</code></h5>
<pre><code class="lang-js"><span class="token keyword">import</span> path from <span class="token string">&apos;path&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> Express from <span class="token string">&apos;express&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> createStore <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Provider <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> counterApp from <span class="token string">&apos;./reducers&apos;</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> App from <span class="token string">&apos;./containers/App&apos;</span><span class="token punctuation">;</span>

<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">Express</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> port <span class="token operator">=</span> <span class="token number">3000</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// &#x6BCF;&#x5F53;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x65F6;&#x90FD;&#x4F1A;&#x89E6;&#x53D1;</span>
app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>handleRender<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">// &#x63A5;&#x4E0B;&#x6765;&#x4F1A;&#x8865;&#x5145;&#x8FD9;&#x90E8;&#x5206;&#x4EE3;&#x7801;</span>
<span class="token keyword">function</span> <span class="token function">handleRender</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/* ... */</span> <span class="token punctuation">}</span>
<span class="token keyword">function</span> <span class="token function">renderFullPage</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment" spellcheck="true">/* ... */</span> <span class="token punctuation">}</span>

app<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span>port<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="%E5%A4%84%E7%90%86%E8%AF%B7%E6%B1%82">&#x5904;&#x7406;&#x8BF7;&#x6C42;</h3>
<p>&#x7B2C;&#x4E00;&#x4EF6;&#x8981;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x5C31;&#x662F;&#x5BF9;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; Redux store &#x5B9E;&#x4F8B;&#x3002;&#x8FD9;&#x4E2A; store &#x60DF;&#x4E00;&#x4F5C;&#x7528;&#x662F;&#x63D0;&#x4F9B;&#x5E94;&#x7528;&#x521D;&#x59CB;&#x7684; state&#x3002;</p>
<p>&#x6E32;&#x67D3;&#x65F6;&#xFF0C;&#x4F7F;&#x7528; <code><span class="token operator">&lt;</span>Provider<span class="token operator">&gt;</span></code> &#x6765;&#x5305;&#x4F4F;&#x6839;&#x7EC4;&#x4EF6; <code><span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span></code>&#xFF0C;&#x4EE5;&#x6B64;&#x6765;&#x8BA9;&#x7EC4;&#x4EF6;&#x6811;&#x4E2D;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x90FD;&#x80FD;&#x8BBF;&#x95EE;&#x5230; store&#xFF0C;&#x5C31;&#x50CF;&#x4E4B;&#x524D;&#x7684;<a href="../basics/UsageWithReact.html">&#x642D;&#x914D; React</a> &#x6559;&#x7A0B;&#x8BB2;&#x7684;&#x90A3;&#x6837;&#x3002;</p>
<p>&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x6700;&#x5173;&#x952E;&#x7684;&#x4E00;&#x6B65;&#x662F;&#x5728;<strong>&#x53D1;&#x9001;&#x54CD;&#x5E94;&#x524D;</strong>&#x6E32;&#x67D3;&#x521D;&#x59CB;&#x7684; HTML&#x3002;&#x8FD9;&#x5C31;&#x8981;&#x4F7F;&#x7528; <a href="https://facebook.github.io/react/docs/top-level-api.html#react.rendertostring" target="_blank">React.renderToString()</a>.</p>
<p>&#x7136;&#x540E;&#x4F7F;&#x7528; <a href="../api/Store.html#getState"><code>store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x4ECE; store &#x5F97;&#x5230;&#x521D;&#x59CB; state&#x3002;<code>renderFullPage</code> &#x51FD;&#x6570;&#x4F1A;&#x4ECB;&#x7ECD;&#x63A5;&#x4E0B;&#x6765;&#x5982;&#x4F55;&#x4F20;&#x9012;&#x3002;</p>
<pre><code class="lang-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> renderToString <span class="token punctuation">}</span> from <span class="token string">&apos;react-dom/server&apos;</span>

<span class="token keyword">function</span> <span class="token function">handleRender</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// &#x521B;&#x5EFA;&#x65B0;&#x7684; Redux store &#x5B9E;&#x4F8B;</span>
  <span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>counterApp<span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// &#x628A;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210;&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="token keyword">const</span> html <span class="token operator">=</span> <span class="token function">renderToString</span><span class="token punctuation">(</span>
    <span class="token operator">&lt;</span>Provider store<span class="token operator">=</span><span class="token punctuation">{</span>store<span class="token punctuation">}</span><span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>Provider<span class="token operator">&gt;</span>
  <span class="token punctuation">)</span>

  <span class="token comment" spellcheck="true">// &#x4ECE; store &#x4E2D;&#x83B7;&#x5F97;&#x521D;&#x59CB; state</span>
  <span class="token keyword">const</span> preloadedState <span class="token operator">=</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment" spellcheck="true">// &#x628A;&#x6E32;&#x67D3;&#x540E;&#x7684;&#x9875;&#x9762;&#x5185;&#x5BB9;&#x53D1;&#x9001;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token function">renderFullPage</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="%E6%B3%A8%E5%85%A5%E5%88%9D%E5%A7%8B%E7%BB%84%E4%BB%B6%E7%9A%84-html-%E5%92%8C-state">&#x6CE8;&#x5165;&#x521D;&#x59CB;&#x7EC4;&#x4EF6;&#x7684; HTML &#x548C; State</h3>
<p>&#x670D;&#x52A1;&#x7AEF;&#x6700;&#x540E;&#x4E00;&#x6B65;&#x5C31;&#x662F;&#x628A;&#x521D;&#x59CB;&#x7EC4;&#x4EF6;&#x7684; HTML &#x548C;&#x521D;&#x59CB; state &#x6CE8;&#x5165;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x80FD;&#x591F;&#x6E32;&#x67D3;&#x7684;&#x6A21;&#x677F;&#x4E2D;&#x3002;&#x5982;&#x4F55;&#x4F20;&#x9012; state &#x5462;&#xFF0C;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A; <code><span class="token operator">&lt;</span>script<span class="token operator">&gt;</span></code> &#x6807;&#x7B7E;&#x6765;&#x628A; <code>preloadedState</code> &#x8D4B;&#x7ED9; <code>window<span class="token punctuation">.</span>__INITIAL_STATE__</code>&#x3002;</p>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>window<span class="token punctuation">.</span>__INITIAL_STATE__</code> &#x83B7;&#x53D6; <code>preloadedState</code>&#x3002;</p>
<p>&#x540C;&#x65F6;&#x4F7F;&#x7528; script &#x6807;&#x7B7E;&#x6765;&#x5F15;&#x5165;&#x6253;&#x5305;&#x540E;&#x7684; js bundle &#x6587;&#x4EF6;&#x3002;&#x8FD9;&#x662F;&#x6253;&#x5305;&#x5DE5;&#x5177;&#x8F93;&#x51FA;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF0C;&#x4EE5;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x6216;&#x8005; URL &#x7684;&#x65B9;&#x5F0F;&#x5B9E;&#x73B0;&#x670D;&#x52A1;&#x7AEF;&#x5F00;&#x53D1;&#x4E2D;&#x7684;&#x70ED;&#x52A0;&#x8F7D;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x4EE3;&#x7801;&#x3002;</p>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">renderFullPage</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> `
    <span class="token operator">&lt;</span><span class="token operator">!</span>doctype html<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>html<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>head<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>title<span class="token operator">&gt;</span>Redux Universal Example<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>body<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>div id<span class="token operator">=</span><span class="token string">&quot;root&quot;</span><span class="token operator">&gt;</span>$<span class="token punctuation">{</span>html<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>script<span class="token operator">&gt;</span>
          window<span class="token punctuation">.</span>__INITIAL_STATE__ <span class="token operator">=</span> $<span class="token punctuation">{</span>JSON<span class="token punctuation">.</span><span class="token function">stringify</span><span class="token punctuation">(</span>preloadedState<span class="token punctuation">)</span><span class="token punctuation">}</span>
        <span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>script src<span class="token operator">=</span><span class="token string">&quot;/static/bundle.js&quot;</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">&gt;</span>
    `
<span class="token punctuation">}</span>
</code></pre>
<h2 id="%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%BC%80%E5%8F%91">&#x5BA2;&#x6237;&#x7AEF;&#x5F00;&#x53D1;</h2>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x4EE3;&#x7801;&#x975E;&#x5E38;&#x76F4;&#x89C2;&#x3002;&#x53EA;&#x9700;&#x8981;&#x4ECE; <code>window<span class="token punctuation">.</span>__INITIAL_STATE__</code> &#x5F97;&#x5230;&#x521D;&#x59CB; state&#xFF0C;&#x5E76;&#x4F20;&#x7ED9; <a href="../api/createStore.html"><code><span class="token function">createStore</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a> &#x51FD;&#x6570;&#x5373;&#x53EF;&#x3002;</p>
<p>&#x4EE3;&#x7801;&#x5982;&#x4E0B;:</p>
<h4 id="-client-js"><code>client<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> React from <span class="token string">&apos;react&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> render <span class="token punctuation">}</span> from <span class="token string">&apos;react-dom&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> createStore <span class="token punctuation">}</span> from <span class="token string">&apos;redux&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Provider <span class="token punctuation">}</span> from <span class="token string">&apos;react-redux&apos;</span>
<span class="token keyword">import</span> App from <span class="token string">&apos;./containers/App&apos;</span>
<span class="token keyword">import</span> counterApp from <span class="token string">&apos;./reducers&apos;</span>

<span class="token comment" spellcheck="true">// &#x901A;&#x8FC7;&#x670D;&#x52A1;&#x7AEF;&#x6CE8;&#x5165;&#x7684;&#x5168;&#x5C40;&#x53D8;&#x91CF;&#x5F97;&#x5230;&#x521D;&#x59CB; state</span>
<span class="token keyword">const</span> preloadedState <span class="token operator">=</span> window<span class="token punctuation">.</span>__INITIAL_STATE__

<span class="token comment" spellcheck="true">// &#x4F7F;&#x7528;&#x521D;&#x59CB; state &#x521B;&#x5EFA; Redux store</span>
<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>counterApp<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span>

<span class="token function">render</span><span class="token punctuation">(</span>
  <span class="token operator">&lt;</span>Provider store<span class="token operator">=</span><span class="token punctuation">{</span>store<span class="token punctuation">}</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>Provider<span class="token operator">&gt;</span><span class="token punctuation">,</span>
  document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">&apos;root&apos;</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span>
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x9009;&#x62E9;&#x81EA;&#x5DF1;&#x559C;&#x6B22;&#x7684;&#x6253;&#x5305;&#x5DE5;&#x5177;&#xFF08;Webpack, Browserify &#x6216;&#x5176;&#x5B83;&#xFF09;&#x6765;&#x7F16;&#x8BD1;&#x5E76;&#x6253;&#x5305;&#x6587;&#x4EF6;&#x5230; <code>dist<span class="token operator">/</span>bundle<span class="token punctuation">.</span>js</code>&#x3002;</p>
<p>&#x5F53;&#x9875;&#x9762;&#x52A0;&#x8F7D;&#x65F6;&#xFF0C;&#x6253;&#x5305;&#x540E;&#x7684; js &#x4F1A;&#x542F;&#x52A8;&#xFF0C;&#x5E76;&#x8C03;&#x7528; <a href="https://facebook.github.io/react/docs/top-level-api.html#react.render" target="_blank"><code>React<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code></a>&#xFF0C;&#x7136;&#x540E;&#x4F1A;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684; HTML &#x7684; <code>data<span class="token operator">-</span>react<span class="token operator">-</span>id</code> &#x5C5E;&#x6027;&#x505A;&#x5173;&#x8054;&#x3002;&#x8FD9;&#x4F1A;&#x628A;&#x65B0;&#x751F;&#x6210;&#x7684; React &#x5B9E;&#x4F8B;&#x4E0E;&#x670D;&#x52A1;&#x7AEF;&#x7684;&#x865A;&#x62DF; DOM &#x8FDE;&#x63A5;&#x8D77;&#x6765;&#x3002;&#x56E0;&#x4E3A;&#x540C;&#x6837;&#x4F7F;&#x7528;&#x4E86;&#x6765;&#x81EA; Redux store &#x7684;&#x521D;&#x59CB; state&#xFF0C;&#x5E76;&#x4E14; view &#x7EC4;&#x4EF6;&#x4EE3;&#x7801;&#x662F;&#x4E00;&#x6837;&#x7684;&#xFF0C;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x5F97;&#x5230;&#x4E86;&#x76F8;&#x540C;&#x7684; DOM&#x3002;</p>
<p>&#x5C31;&#x662F;&#x8FD9;&#x6837;&#xFF01;&#x8FD9;&#x5C31;&#x662F;&#x5B9E;&#x73B0;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x7684;&#x6240;&#x6709;&#x6B65;&#x9AA4;&#x3002;</p>
<p>&#x4F46;&#x8FD9;&#x6837;&#x505A;&#x8FD8;&#x662F;&#x6BD4;&#x8F83;&#x539F;&#x59CB;&#x7684;&#x3002;&#x53EA;&#x4F1A;&#x7528;&#x52A8;&#x6001;&#x4EE3;&#x7801;&#x6E32;&#x67D3;&#x4E00;&#x4E2A;&#x9759;&#x6001;&#x7684; View&#x3002;&#x4E0B;&#x4E00;&#x6B65;&#x8981;&#x505A;&#x7684;&#x662F;&#x52A8;&#x6001;&#x521B;&#x5EFA;&#x521D;&#x59CB; state &#x652F;&#x6301;&#x52A8;&#x6001;&#x6E32;&#x67D3; view&#x3002;</p>
<h2 id="%E5%87%86%E5%A4%87%E5%88%9D%E5%A7%8B-state">&#x51C6;&#x5907;&#x521D;&#x59CB; State</h2>
<p>&#x56E0;&#x4E3A;&#x5BA2;&#x6237;&#x7AEF;&#x53EA;&#x662F;&#x6267;&#x884C;&#x6536;&#x5230;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x521A;&#x5F00;&#x59CB;&#x7684;&#x521D;&#x59CB; state &#x53EF;&#x80FD;&#x662F;&#x7A7A;&#x7684;&#xFF0C;&#x7136;&#x540E;&#x6839;&#x636E;&#x9700;&#x8981;&#x83B7;&#x53D6; state&#x3002;&#x5728;&#x670D;&#x52A1;&#x7AEF;&#xFF0C;&#x6E32;&#x67D3;&#x662F;&#x540C;&#x6B65;&#x6267;&#x884C;&#x7684;&#x800C;&#x4E14;&#x6211;&#x4EEC;&#x53EA;&#x6709;&#x4E00;&#x6B21;&#x6E32;&#x67D3; view &#x7684;&#x673A;&#x4F1A;&#x3002;&#x5728;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x6839;&#x636E;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x6216;&#x8005;&#x5916;&#x90E8; state&#xFF08;&#x5982;&#x8BBF;&#x95EE; API &#x6216;&#x8005;&#x6570;&#x636E;&#x5E93;&#xFF09;&#xFF0C;&#x8BA1;&#x7B97;&#x540E;&#x5F97;&#x5230;&#x521D;&#x59CB; state&#x3002;</p>
<h3 id="%E5%A4%84%E7%90%86-request-%E5%8F%82%E6%95%B0">&#x5904;&#x7406; Request &#x53C2;&#x6570;</h3>
<p>&#x670D;&#x52A1;&#x7AEF;&#x6536;&#x5230;&#x7684;&#x552F;&#x4E00;&#x8F93;&#x5165;&#x662F;&#x6765;&#x81EA;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x8BF7;&#x6C42;&#x3002;&#x5728;&#x670D;&#x52A1;&#x5668;&#x542F;&#x52A8;&#x65F6;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x505A;&#x4E00;&#x4E9B;&#x914D;&#x7F6E;&#xFF08;&#x5982;&#x8FD0;&#x884C;&#x5728;&#x5F00;&#x53D1;&#x73AF;&#x5883;&#x8FD8;&#x662F;&#x751F;&#x4EA7;&#x73AF;&#x5883;&#xFF09;&#xFF0C;&#x4F46;&#x8FD9;&#x4E9B;&#x914D;&#x7F6E;&#x662F;&#x9759;&#x6001;&#x7684;&#x3002;</p>
<p>&#x8BF7;&#x6C42;&#x4F1A;&#x5305;&#x542B; URL &#x8BF7;&#x6C42;&#x76F8;&#x5173;&#x4FE1;&#x606F;&#xFF0C;&#x5305;&#x62EC;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#xFF0C;&#x5B83;&#x4EEC;&#x5BF9;&#x4E8E;&#x505A; <a href="https://github.com/rackt/react-router" target="_blank">React Router</a> &#x8DEF;&#x7531;&#x65F6;&#x53EF;&#x80FD;&#x4F1A;&#x6709;&#x7528;&#x3002;&#x4E5F;&#x53EF;&#x80FD;&#x5728;&#x8BF7;&#x6C42;&#x5934;&#x91CC;&#x5305;&#x542B; cookies&#xFF0C;&#x9274;&#x6743;&#x4FE1;&#x606F;&#x6216;&#x8005; POST &#x5185;&#x5BB9;&#x6570;&#x636E;&#x3002;&#x4E0B;&#x9762;&#x6F14;&#x793A;&#x5982;&#x4F55;&#x57FA;&#x4E8E;&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x6765;&#x5F97;&#x5230;&#x521D;&#x59CB; state&#x3002;</p>
<h4 id="-server-js"><code>server<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">import</span> qs from <span class="token string">&apos;qs&apos;</span><span class="token punctuation">;</span> <span class="token comment" spellcheck="true">// &#x6DFB;&#x52A0;&#x5230;&#x6587;&#x4EF6;&#x5F00;&#x5934;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> renderToString <span class="token punctuation">}</span> from <span class="token string">&apos;react-dom/server&apos;</span>

<span class="token keyword">function</span> <span class="token function">handleRender</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// &#x5982;&#x679C;&#x5B58;&#x5728;&#x7684;&#x8BDD;&#xFF0C;&#x4ECE; request &#x8BFB;&#x53D6; counter</span>
  <span class="token keyword">const</span> params <span class="token operator">=</span> qs<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>query<span class="token punctuation">)</span>
  <span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span>counter<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token number">0</span>

  <span class="token comment" spellcheck="true">// &#x5F97;&#x5230;&#x521D;&#x59CB; state</span>
  <span class="token keyword">let</span> preloadedState <span class="token operator">=</span> <span class="token punctuation">{</span> counter <span class="token punctuation">}</span>

  <span class="token comment" spellcheck="true">// &#x521B;&#x5EFA;&#x65B0;&#x7684; Redux store &#x5B9E;&#x4F8B;</span>
  <span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>counterApp<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span>

  <span class="token comment" spellcheck="true">// &#x628A;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210;&#x5B57;&#x7B26;&#x4E32;</span>
  <span class="token keyword">const</span> html <span class="token operator">=</span> <span class="token function">renderToString</span><span class="token punctuation">(</span>
    <span class="token operator">&lt;</span>Provider store<span class="token operator">=</span><span class="token punctuation">{</span>store<span class="token punctuation">}</span><span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span><span class="token operator">/</span>Provider<span class="token operator">&gt;</span>
  <span class="token punctuation">)</span>

  <span class="token comment" spellcheck="true">// &#x4ECE; Redux store &#x5F97;&#x5230;&#x521D;&#x59CB; state</span>
  <span class="token keyword">const</span> finalState <span class="token operator">=</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

  <span class="token comment" spellcheck="true">// &#x628A;&#x6E32;&#x67D3;&#x540E;&#x7684;&#x9875;&#x9762;&#x53D1;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token function">renderFullPage</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> finalState<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x4E0A;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x9996;&#x5148;&#x8BBF;&#x95EE; Express &#x7684; <code>Request</code> &#x5BF9;&#x8C61;&#x3002;&#x628A;&#x53C2;&#x6570;&#x8F6C;&#x6210;&#x6570;&#x5B57;&#xFF0C;&#x7136;&#x540E;&#x8BBE;&#x7F6E;&#x5230;&#x521D;&#x59CB; state &#x4E2D;&#x3002;&#x5982;&#x679C;&#x4F60;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8BBF;&#x95EE; <a href="http://localhost:3000/?counter=100" target="_blank">http://localhost:3000/?counter=100</a>&#xFF0C;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x8BA1;&#x6570;&#x5668;&#x4ECE; 100 &#x5F00;&#x59CB;&#x3002;&#x5728;&#x6E32;&#x67D3;&#x540E;&#x7684; HTML &#x4E2D;&#xFF0C;&#x4F60;&#x4F1A;&#x770B;&#x5230;&#x8BA1;&#x6570;&#x663E;&#x793A; 100 &#x540C;&#x65F6;&#x8BBE;&#x7F6E;&#x8FDB;&#x4E86; <code>__INITIAL_STATE__</code> &#x53D8;&#x91CF;&#x3002;</p>
<h3 id="%E8%8E%B7%E5%8F%96%E5%BC%82%E6%AD%A5-state">&#x83B7;&#x53D6;&#x5F02;&#x6B65; State</h3>
<p>&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x5E38;&#x7528;&#x7684;&#x573A;&#x666F;&#x662F;&#x5904;&#x7406;&#x5F02;&#x6B65; state&#x3002;&#x56E0;&#x4E3A;&#x670D;&#x52A1;&#x7AEF;&#x6E32;&#x67D3;&#x5929;&#x751F;&#x662F;&#x540C;&#x6B65;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x5F02;&#x6B65;&#x7684;&#x6570;&#x636E;&#x83B7;&#x53D6;&#x64CD;&#x4F5C;&#x5BF9;&#x5E94;&#x5230;&#x540C;&#x6B65;&#x64CD;&#x4F5C;&#x975E;&#x5E38;&#x91CD;&#x8981;&#x3002;</p>
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x5F80;&#x540C;&#x6B65;&#x4EE3;&#x7801;&#x91CC;&#x4F20;&#x9012;&#x4E00;&#x4E9B;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x91CC;&#x5F15;&#x7528;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#xFF0C;&#x628A;&#x6E32;&#x67D3;&#x540E;&#x7684; HTML &#x53D1;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x4E0D;&#x8981;&#x62C5;&#x5FC3;&#xFF0C;&#x5E76;&#x6CA1;&#x6709;&#x60F3;&#x50CF;&#x4E2D;&#x90A3;&#x4E48;&#x96BE;&#x3002;</p>
<p>&#x672C;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A;&#x5916;&#x90E8;&#x6570;&#x636E;&#x6E90;&#x63D0;&#x4F9B;&#x8BA1;&#x7B97;&#x5668;&#x7684;&#x521D;&#x59CB;&#x503C;&#xFF08;&#x6240;&#x8C13;&#x7684;&#x628A;&#x8BA1;&#x7B97;&#x4F5C;&#x4E3A;&#x4E00;&#x79CD;&#x670D;&#x52A1;&#xFF09;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x6A21;&#x62DF;&#x4E00;&#x4E2A;&#x8BF7;&#x6C42;&#x5E76;&#x4F7F;&#x7528;&#x7ED3;&#x679C;&#x521B;&#x5EFA;&#x521D;&#x59CB; state&#x3002;API &#x8BF7;&#x6C42;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<h4 id="-api-counter-js"><code>api<span class="token operator">/</span>counter<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token keyword">function</span> <span class="token function">getRandomInt</span><span class="token punctuation">(</span>min<span class="token punctuation">,</span> max<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">(</span>max <span class="token operator">-</span> min<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">+</span> min
<span class="token punctuation">}</span>

<span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">fetchCounter</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token function">callback</span><span class="token punctuation">(</span><span class="token function">getRandomInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x518D;&#x6B21;&#x8BF4;&#x660E;&#x4E00;&#x4E0B;&#xFF0C;&#x8FD9;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x62DF;&#x7684; API&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>setTimeout</code> &#x6A21;&#x62DF;&#x4E00;&#x4E2A;&#x9700;&#x8981; 500 &#x6BEB;&#x79D2;&#x7684;&#x8BF7;&#x6C42;&#xFF08;&#x5B9E;&#x9645;&#x9879;&#x76EE;&#x4E2D; API &#x8BF7;&#x6C42;&#x4E00;&#x822C;&#x4F1A;&#x66F4;&#x5FEB;&#xFF09;&#x3002;&#x4F20;&#x5165;&#x4E00;&#x4E2A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x5F02;&#x6B65;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x6570;&#x5B57;&#x3002;&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x4E86;&#x57FA;&#x4E8E; Promise &#x7684; API &#x5DE5;&#x5177;&#xFF0C;&#x90A3;&#x4E48;&#x8981;&#x628A;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x653E;&#x5230; <code>then</code> &#x4E2D;&#x3002;</p>
<p>&#x5728;&#x670D;&#x52A1;&#x7AEF;&#xFF0C;&#x628A;&#x4EE3;&#x7801;&#x4F7F;&#x7528; <code>fetchCounter</code> &#x5305;&#x8D77;&#x6765;&#xFF0C;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x91CC;&#x62FF;&#x5230;&#x7ED3;&#x679C;&#xFF1A;</p>
<h4 id="-server-js"><code>server<span class="token punctuation">.</span>js</code></h4>
<pre><code class="lang-js"><span class="token comment" spellcheck="true">// &#x6DFB;&#x52A0;&#x5230; import</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> fetchCounter <span class="token punctuation">}</span> from <span class="token string">&apos;./api/counter&apos;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> renderToString <span class="token punctuation">}</span> from <span class="token string">&apos;react-dom/server&apos;</span>

<span class="token keyword">function</span> <span class="token function">handleRender</span><span class="token punctuation">(</span>req<span class="token punctuation">,</span> res<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment" spellcheck="true">// &#x5F02;&#x6B65;&#x8BF7;&#x6C42;&#x6A21;&#x62DF;&#x7684; API</span>
  <span class="token function">fetchCounter</span><span class="token punctuation">(</span>apiResult <span class="token operator">=</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">// &#x5982;&#x679C;&#x5B58;&#x5728;&#x7684;&#x8BDD;&#xFF0C;&#x4ECE; request &#x8BFB;&#x53D6; counter</span>
    <span class="token keyword">const</span> params <span class="token operator">=</span> qs<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>query<span class="token punctuation">)</span>
    <span class="token keyword">const</span> counter <span class="token operator">=</span> <span class="token function">parseInt</span><span class="token punctuation">(</span>params<span class="token punctuation">.</span>counter<span class="token punctuation">)</span> <span class="token operator">||</span> apiResult <span class="token operator">||</span> <span class="token number">0</span>

    <span class="token comment" spellcheck="true">// &#x5F97;&#x5230;&#x521D;&#x59CB; state</span>
    <span class="token keyword">let</span> preloadedState <span class="token operator">=</span> <span class="token punctuation">{</span> counter <span class="token punctuation">}</span>

    <span class="token comment" spellcheck="true">// &#x521B;&#x5EFA;&#x65B0;&#x7684; Redux store &#x5B9E;&#x4F8B;</span>
    <span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token function">createStore</span><span class="token punctuation">(</span>counterApp<span class="token punctuation">,</span> preloadedState<span class="token punctuation">)</span>

    <span class="token comment" spellcheck="true">// &#x628A;&#x7EC4;&#x4EF6;&#x6E32;&#x67D3;&#x6210;&#x5B57;&#x7B26;&#x4E32;</span>
    <span class="token keyword">const</span> html <span class="token operator">=</span> <span class="token function">renderToString</span><span class="token punctuation">(</span>
      <span class="token operator">&lt;</span>Provider store<span class="token operator">=</span><span class="token punctuation">{</span>store<span class="token punctuation">}</span><span class="token operator">&gt;</span>
        <span class="token operator">&lt;</span>App <span class="token operator">/</span><span class="token operator">&gt;</span>
      <span class="token operator">&lt;</span><span class="token operator">/</span>Provider<span class="token operator">&gt;</span>
    <span class="token punctuation">)</span>

    <span class="token comment" spellcheck="true">// &#x4ECE; Redux store &#x5F97;&#x5230;&#x521D;&#x59CB; state</span>
    <span class="token keyword">const</span> finalState <span class="token operator">=</span> store<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment" spellcheck="true">// &#x628A;&#x6E32;&#x67D3;&#x540E;&#x7684;&#x9875;&#x9762;&#x53D1;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;</span>
    res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token function">renderFullPage</span><span class="token punctuation">(</span>html<span class="token punctuation">,</span> finalState<span class="token punctuation">)</span><span class="token punctuation">)</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x56E0;&#x4E3A;&#x5728;&#x56DE;&#x8C03;&#x4E2D;&#x4F7F;&#x7528;&#x4E86; <code>res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x4FDD;&#x62A4;&#x8FDE;&#x63A5;&#x6253;&#x5F00;&#x5E76;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x6267;&#x884C;&#x524D;&#x4E0D;&#x53D1;&#x9001;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x3002;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x6709; 500ms &#x7684;&#x5EF6;&#x65F6;&#x3002;&#x66F4;&#x9AD8;&#x7EA7;&#x7684;&#x7528;&#x6CD5;&#x4F1A;&#x5305;&#x62EC;&#x5BF9; API &#x8BF7;&#x6C42;&#x51FA;&#x9519;&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x6BD4;&#x5982;&#x9519;&#x8BEF;&#x7684;&#x8BF7;&#x6C42;&#x6216;&#x8005;&#x8D85;&#x65F6;&#x3002;</p>
<h3 id="%E5%AE%89%E5%85%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9">&#x5B89;&#x5168;&#x6CE8;&#x610F;&#x4E8B;&#x9879;</h3>
<p>&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x4EE3;&#x7801;&#x4E2D;&#x5F88;&#x591A;&#x662F;&#x57FA;&#x4E8E;&#x7528;&#x6237;&#x751F;&#x6210;&#x5185;&#x5BB9;&#xFF08;UGC&#xFF09;&#x548C;&#x8F93;&#x5165;&#x7684;&#xFF0C;&#x4E0D;&#x77E5;&#x4E0D;&#x89C9;&#x4E2D;&#xFF0C;&#x63D0;&#x9AD8;&#x4E86;&#x5E94;&#x7528;&#x53EF;&#x80FD;&#x53D7;&#x653B;&#x51FB;&#x533A;&#x57DF;&#x3002;&#x4EFB;&#x4F55;&#x5E94;&#x7528;&#x90FD;&#x5E94;&#x8BE5;&#x5BF9;&#x7528;&#x6237;&#x8F93;&#x5165;&#x505A;&#x5B89;&#x5168;&#x5904;&#x7406;&#x4EE5;&#x907F;&#x514D;&#x8DE8;&#x7AD9;&#x811A;&#x672C;&#x653B;&#x51FB;&#xFF08;XSS&#xFF09;&#x6216;&#x8005;&#x4EE3;&#x7801;&#x6CE8;&#x5165;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x53EA;&#x5BF9;&#x5B89;&#x5168;&#x505A;&#x57FA;&#x672C;&#x5904;&#x7406;&#x3002;&#x5F53;&#x4ECE;&#x8BF7;&#x6C42;&#x4E2D;&#x62FF;&#x53C2;&#x6570;&#x65F6;&#xFF0C;&#x5BF9; <code>counter</code> &#x53C2;&#x6570;&#x4F7F;&#x7528; <code>parseInt</code> &#x628A;&#x5B83;&#x8F6C;&#x6210;&#x6570;&#x5B57;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x8FD9;&#x6837;&#x505A;&#xFF0C;&#x5F53; request &#x4E2D;&#x6709; script &#x6807;&#x7B7E;&#x65F6;&#xFF0C;&#x5F88;&#x5BB9;&#x6613;&#x5728;&#x6E32;&#x67D3;&#x7684; HTML &#x4E2D;&#x751F;&#x6210;&#x5371;&#x9669;&#x4EE3;&#x7801;&#x3002;&#x5C31;&#x50CF;&#x8FD9;&#x6837;&#x7684;&#xFF1A;<code><span class="token operator">?</span>counter<span class="token operator">=</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span><span class="token operator">&lt;</span>script<span class="token operator">&gt;</span><span class="token function">doSomethingBad</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token operator">&lt;</span><span class="token operator">/</span>script<span class="token operator">&gt;</span></code></p>
<p>&#x5728;&#x6211;&#x4EEC;&#x6781;&#x7B80;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x628A;&#x8F93;&#x5165;&#x8F6C;&#x6210;&#x6570;&#x5B57;&#x5DF2;&#x7ECF;&#x6BD4;&#x8F83;&#x5B89;&#x5168;&#x3002;&#x5982;&#x679C;&#x5904;&#x7406;&#x66F4;&#x590D;&#x6742;&#x7684;&#x8F93;&#x5165;&#xFF0C;&#x6BD4;&#x5982;&#x81EA;&#x5B9A;&#x4E49;&#x683C;&#x5F0F;&#x7684;&#x6587;&#x672C;&#xFF0C;&#x4F60;&#x5E94;&#x8BE5;&#x7528;&#x5B89;&#x5168;&#x51FD;&#x6570;&#x5904;&#x7406;&#x8F93;&#x5165;&#xFF0C;&#x6BD4;&#x5982; <a href="https://www.npmjs.com/package/validator" target="_blank">validator.js</a>&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x53EF;&#x80FD;&#x6DFB;&#x52A0;&#x989D;&#x5916;&#x7684;&#x5B89;&#x5168;&#x5C42;&#x6765;&#x5BF9;&#x4EA7;&#x751F;&#x7684; state &#x8FDB;&#x884C;&#x6D88;&#x6BD2;&#x3002;<code>JSON<span class="token punctuation">.</span>stringify</code> &#x53EF;&#x80FD;&#x4F1A;&#x9020;&#x6210; script &#x6CE8;&#x5165;&#x3002;&#x9274;&#x4E8E;&#x6B64;&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x6E05;&#x6D17; JSON &#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7684; HTML &#x6807;&#x7B7E;&#x548C;&#x5176;&#x5B83;&#x5371;&#x9669;&#x7684;&#x5B57;&#x7B26;&#x3002;&#x53EF;&#x80FD;&#x901A;&#x8FC7;&#x5B57;&#x7B26;&#x4E32;&#x66FF;&#x6362;&#x6216;&#x8005;&#x4F7F;&#x7528;&#x590D;&#x6742;&#x7684;&#x5E93;&#x5982; <a href="https://github.com/yahoo/serialize-javascript" target="_blank">serialize-javascript</a> &#x5904;&#x7406;&#x3002;</p>
<h2 id="%E4%B8%8B%E4%B8%80%E6%AD%A5">&#x4E0B;&#x4E00;&#x6B65;</h2>
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x53C2;&#x8003; <a href="../advanced/AsyncActions.html">&#x5F02;&#x6B65; Actions</a> &#x5B66;&#x4E60;&#x66F4;&#x591A;&#x4F7F;&#x7528; Promise &#x548C; thunk &#x8FD9;&#x4E9B;&#x5F02;&#x6B65;&#x5143;&#x7D20;&#x6765;&#x8868;&#x793A;&#x5F02;&#x6B65;&#x6570;&#x636E;&#x6D41;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x8BB0;&#x4F4F;&#xFF0C;&#x90A3;&#x91CC;&#x5B66;&#x5230;&#x7684;&#x4EFB;&#x4F55;&#x5185;&#x5BB9;&#x90FD;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x540C;&#x6784;&#x6E32;&#x67D3;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4F60;&#x4F7F;&#x7528;&#x4E86; <a href="https://github.com/rackt/react-router" target="_blank">React Router</a>&#xFF0C;&#x4F60;&#x53EF;&#x80FD;&#x8FD8;&#x9700;&#x8981;&#x5728;&#x8DEF;&#x7531;&#x5904;&#x7406;&#x7EC4;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x9759;&#x6001;&#x7684; <code><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x65B9;&#x6CD5;&#x6765;&#x83B7;&#x53D6;&#x4F9D;&#x8D56;&#x7684;&#x6570;&#x636E;&#x3002;&#x5B83;&#x53EF;&#x80FD;&#x8FD4;&#x56DE; <a href="../advanced/AsyncActions.html">&#x5F02;&#x6B65; action</a>&#xFF0C;&#x4EE5;&#x4FBF;&#x4F60;&#x7684; <code>handleRender</code> &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5339;&#x914D;&#x5230;&#x5BF9;&#x5E94;&#x7684;&#x7EC4;&#x4EF6;&#x7C7B;&#xFF0C;&#x5BF9;&#x5B83;&#x4EEC;&#x5747; dispatch <code><span class="token function">fetchData</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code> &#x7684;&#x7ED3;&#x679C;&#xFF0C;&#x5728; Promise &#x89E3;&#x51B3;&#x540E;&#x624D;&#x6E32;&#x67D3;&#x3002;&#x8FD9;&#x6837;&#x4E0D;&#x540C;&#x8DEF;&#x7531;&#x9700;&#x8981;&#x8C03;&#x7528;&#x7684; API &#x8BF7;&#x6C42;&#x90FD;&#x5E76;&#x7F6E;&#x4E8E;&#x8DEF;&#x7531;&#x5904;&#x7406;&#x7EC4;&#x4EF6;&#x4E86;&#x3002;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x540C;&#x6837;&#x6280;&#x672F;&#x6765;&#x907F;&#x514D;&#x5728;&#x5207;&#x6362;&#x9875;&#x9762;&#x65F6;&#xFF0C;&#x5F53;&#x6570;&#x636E;&#x8FD8;&#x6CA1;&#x6709;&#x52A0;&#x8F7D;&#x5B8C;&#x6210;&#x524D;&#x6267;&#x884C;&#x8DEF;&#x7531;&#x3002;</p>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../docs/recipes/ReducingBoilerplate.html" class="navigation navigation-prev " aria-label="Previous page: 减少样板代码"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../docs/recipes/WritingTests.html" class="navigation navigation-next " aria-label="Next page: 编写测试"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-edit-link/plugin.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-ga/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontSettings":{"theme":null,"family":"sans","size":2},"edit-link":{"base":"https://github.com/camsong/redux-in-chinese/tree/master","label":"开始纠错"},"ga":{"token":"UA-66122997-1","configuration":"auto"}};
    gitbook.start(config);
});
</script>

        <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-66122997-1', 'auto');ga('send', 'pageview');</script>
    </body>
    
</html>
